package com.yanwq.nwa;

import android.app.Application;
import android.content.Context;

import com.yanwq.nwa.entity.DaoMaster;
import com.yanwq.nwa.entity.DaoSession;
import com.yanwq.nwa.im.netty.WebSocketClientRunner;

import org.greenrobot.greendao.database.Database;

import java.net.URI;

/**
 * Author: Yan
 * Date: 2017/3/21 0021
 */

public class MApplication extends Application {
    private static MApplication context;
    private static WebSocketClientRunner webSocketClientRunner;
    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        initContext();
        initWebSocketClientRunner();

        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "chat-db");
        Database db = helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();
    }

    private void initContext() {
        context = this;
    }

    private void initWebSocketClientRunner() {
        try {
            URI uri = new URI("ws://192.168.191.1:8080/websocket");
            webSocketClientRunner = new WebSocketClientRunner(uri);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Context getContext() {
        return context;
    }

    public static WebSocketClientRunner getWebSocketClientRunner() {
        return webSocketClientRunner;
    }

    public static DaoSession getDaoSession() {
        return daoSession;
    }
}
